﻿using Microsoft.Extensions.DependencyInjection;
using System.Text;

namespace MyTRCP.Common
{
    public static class Tools
    {
        public static short Int16FromBEBA(byte[] data, int pos = 0)
        {
            if (data.Length - pos < 2)
            {
                throw new ArgumentException("数组长度不足", nameof(data));
            }
            return (short)(data[pos] << 8 | data[pos + 1]);
        }
        public static byte[] Int16ToBEBA(short i)
        {
            return [(byte)(i >> 8), (byte)i];
        }
        public static byte[] Int16ToBEBA(short i, byte[] source, int pos)
        {
            if (source.Length - pos < 2)
            {
                throw new ArgumentException("数组长度不足", nameof(source));
            }
            source[pos] = (byte)(i >> 8);
            source[pos + 1] = (byte)i;
            return source;
        }
        public static byte[] Int16ToBEBA(ushort i, byte[] source, int pos)
        {
            if (source.Length - pos < 2)
            {
                throw new ArgumentException("数组长度不足", nameof(source));
            }
            source[pos] = (byte)(i >> 8);
            source[pos + 1] = (byte)i;
            return source;
        }
        public static int Int32FromBEBA(byte[] data, int pos = 0)
        {
            if (data.Length - pos < 4)
            {
                throw new ArgumentException("数组长度不足", nameof(data));
            }
            return data[pos] << 24 | data[pos + 1] << 16 | data[pos + 2] << 8 | data[pos + 3];
        }
        public static byte[] Int32ToBEBA(int i)
        {
            return [(byte)(i >> 24), (byte)(i >> 16), (byte)(i >> 8), (byte)i];
        }
        public static byte[] Int32ToBEBA(int i, byte[] source, int pos)
        {
            if (source.Length - pos < 4)
            {
                throw new ArgumentException("数组长度不足", nameof(source));
            }
            source[pos] = (byte)(i >> 24);
            source[pos + 1] = (byte)(i >> 16);
            source[pos + 2] = (byte)(i >> 8);
            source[pos + 3] = (byte)i;
            return source;
        }
        public static ushort UInt16FromBEBA(byte[] data, int pos = 0)
        {
            if (data.Length - pos < 2)
            {
                throw new ArgumentException("数组长度不足", nameof(data));
            }
            return (ushort)(data[pos] << 8 | data[pos + 1]);
        }
        public static byte[] UInt16ToBEBA(ushort i)
        {
            return [(byte)(i >> 8), (byte)i];
        }
        public static uint UInt32FromBEBA(byte[] data, int pos = 0)
        {
            if (data.Length - pos < 4)
            {
                throw new ArgumentException("数组长度不足", nameof(data));
            }
            return (uint)(data[pos] << 24 | data[pos + 1] << 16 | data[pos + 2] << 8 | data[pos + 3]);
        }
        public static byte[] UInt32ToBEBA(uint i)
        {
            return [(byte)(i >> 24), (byte)(i >> 16), (byte)(i >> 8), (byte)i];
        }
        public static byte[] UInt32ToBEBA(uint i, byte[] source, int pos)
        {
            if (source.Length - pos < 4)
            {
                throw new ArgumentException("数组长度不足", nameof(source));
            }
            source[pos] = (byte)(i >> 24);
            source[pos + 1] = (byte)(i >> 16);
            source[pos + 2] = (byte)(i >> 8);
            source[pos + 3] = (byte)i;
            return source;
        }
        public static long GetLocalNowTimeStamp()
        {
            return DateTimeOffset.UtcNow.ToUnixTimeSeconds() + (long)DateTimeOffset.Now.Offset.TotalSeconds;
        }
        public static long GetUtcNowTimeStamp()
        {
            return DateTimeOffset.UtcNow.ToUnixTimeSeconds();
        }
        public static string ByteArray2HexString(byte[] data)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sb.AppendFormat("{0:x2}", data[i]);
                if (i < data.Length - 1)
                {
                    sb.Append(' ');
                }
            }
            return sb.ToString();
        }
        public static string BufferBytes2HexString(byte[] data, int pos, int length)
        {
            StringBuilder sb = new StringBuilder();
            int finalLen = Math.Min(length, data.Length - pos);
            for (int i = pos; i < pos + finalLen; i++)
            {
                sb.AppendFormat("{0:x2}", data[i]);
                if (i < pos + finalLen - 1)
                {
                    sb.Append(' ');
                }
            }
            return sb.ToString();
        }
        public static byte ComputeChecksum(byte[] data, int pos, int len)
        {
            byte checksum = 0;
            for (int i = pos; i < len; i++)
            {
                checksum = (byte)(checksum ^ data[i]);
            }
            return checksum;
        }
    }
}